#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2022-11-01 14:06:58
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

import time
import network
import espnow
import machine
import ubinascii




#开关
# A WLAN interface must be active to send()/recv()
def main():
    # IF_INDEX_STA = network.STA_IF
	# IF_INDEX_AP = network.AP_IF
    # ap = network.WLAN(network.AP_IF)
    # ap.active(True)
    # network.WLAN(network.AP_IF).active(True) # Or network.STA_IF

    sta = network.WLAN(network.STA_IF)  # Or network.AP_IF
    sta.active(True)

    mac = ubinascii.hexlify(sta.config('mac'),':').decode()
    print(mac)
    # sta.disconnect()   # For ESP8266

    e = espnow.ESPNow()
    e.active(True)
    # allpeers = b'\xff\xff\xff\xff\xff\xff'
    # testpeer = b'\x84\x0d\x8e\x99\xdb\xc0'
    # peers = [allpeers]
    
    # maddr = espnow.get_mac_addr()
    # print(maddr)

    bcast = b'\xff' * 6
    e.add_peer(bcast)
    
    # for i,v in enumerate(peers):
    #     e.add_peer(v)
    print('start contorler...')
    #按键检测循环
    while True:
        # e.send(testpeer, 'cmdtest'.encode())
        # e.send('cmdtest'.encode())
        # e.send(bcast, "Hello World!")
        host, msg = e.recv()
        if msg:
            print('get msg',msg.decode())
            print(host)
        time.sleep_ms(1)
        
if __name__ == '__main__':
    main()